﻿using OASystem.BLL.IService;
using OASystem.Common;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace OASystem.BLL.Service
{
    //记得修改Userinfo成自己要使用的实体
    public class PersonService : BaseRepository<Userinfo>, IPersonService
    {
        public static Userinfo user;
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult UserPageList(int page, int limit, string username)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            int count = 0;
            List<Userinfo> loginInfos = new List<Userinfo>();
            Expression<Func<Userinfo, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(username))
            {
                whereLambda = a => a.UserName.Contains(username);
            }
            Expression<Func<Userinfo, int>> orderbyLambda = c => c.UserId;
            responseResult.data = QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
            responseResult.count = count;
            return responseResult;
        }
        /// <summary>
        /// 显示编辑数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult Update(int id)
        {
            ResponseResult responseResult = new ResponseResult();

            Expression<Func<Userinfo, bool>> whereLambda = a => a.UserId == id;
            if (Query(whereLambda) != null)
            {
                responseResult.success = true;
            }

            responseResult.data = Query(whereLambda);
            return responseResult;
        }
        BaseRepository<Adminuserrole> repository = new BaseRepository<Adminuserrole>();
        /// <summary>
        /// 多表添加
        /// </summary>
        /// <param name="userinfo"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public ResponseResult MoreAdd(Userinfo userinfo, int pos)
        {
            using (var db = new OasystemEntities1())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        ///添加第一张表
                        userinfo.Password = MD5Encrypt.Encrypt(userinfo.Password);
                        userinfo.CreateTime = DateTime.Now;
                        db.Userinfo.Add(userinfo);
                        db.SaveChanges();
                        ///添加第二张表
                        Adminuserrole adminuserrole = new Adminuserrole();
                        adminuserrole.PosId = pos;
                        adminuserrole.UserId = userinfo.UserId;
                        adminuserrole.CreateTime = DateTime.Now;
                        db.Adminuserrole.Add(adminuserrole);

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                    }
                }
            }
            ResponseResult responseResult = new ResponseResult();
            responseResult.success = true;
            return responseResult;
        }
        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="userinfo"></param>
        /// <param name="adminuserrole"></param>
        /// <returns></returns>
        public ResponseResult Delect(Userinfo userinfo, Adminuserrole adminuserrole)
        {
            using (var db = new OasystemEntities1())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        ///删除第一张表
                        db.Entry(userinfo).State = EntityState.Deleted;
                        //删除第二张表                       
                        db.Entry(adminuserrole).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                    }
                }
            }
            ResponseResult responseResult = new ResponseResult();
            responseResult.success = true;
            return responseResult;
        }
        /// <summary>
        /// 多表修改
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <param name="order_Detail"></param>
        /// <returns></returns>
        //public ResponseResult EditUserInfo(Userinfo userinfo, Adminuserrole adminuserrole,int id)
        //{
        //    //创建实例
        //    using (var db = new OasystemEntities1())
        //    {
        //        using (var tran = db.Database.BeginTransaction())
        //        {
        //            try
        //            {
        //                //第一种事务
        //                //修改第一张表
        //                db.Entry(userinfo).State = EntityState.Modified;
        //                db.SaveChanges();
        //                //修改第二张表

                       
        //                adminuserrole.Id = id;
        //                db.Adminuserrole.a(adminuserrole);
        //                //保存


        //                //提交
        //                tran.Commit();
        //            }
        //            catch (Exception ex)
        //            {
        //                //回滚
        //                tran.Rollback();
        //            }
        //        }
        //    }
        //    ResponseResult responseResult = new ResponseResult();
        //    responseResult.success = true;
        //    return responseResult;
        //}
        public ResponseResult Excel()
        {
            ResponseResult result = new ResponseResult();
            Expression<Func<Userinfo, bool>> whereLambda = a => true;
            result.data= QueryList(whereLambda).ToList();
            return result;
        }
    }
}